/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            assetInventoryInInfo: {
                shId: '',
                invTime: '',
                shopHouses: [],
                inoutOrderProducts: [],
                remark: '',
                shopId: vc.getCurrentCommunity().shopId
            }
        },
        _initMethod: function () {
            vc.initDate('invTime', function (_value) {
                $that.assetInventoryInInfo.invTime = _value;
            });
            $that._listShopHouses();
        },
        _initEvent: function () {
            vc.on('assetInventoryIn', 'chooseProductAndSpec', function (_product) {
                _product.invProfit = 0;
                _product.invLoss = 0;
                _product.invProfitMoney = 0;
                _product.invLossMoney = 0;
                _product.invQuantity = 0;
                _product.willExpire = 0;
                _product.attrition = 0;
                _product.quantity = _product.defaultSpecValue.stock;
                $that.assetInventoryInInfo.inoutOrderProducts.push(_product);
            });
        },
        methods: {
            _doAssetInventory: function () {
                let _inoutOrderProducts = $that.assetInventoryInInfo.inoutOrderProducts;
                if (_inoutOrderProducts.length < 1) {
                    vc.toast('请选择商品');
                    return;
                }
                _inoutOrderProducts.forEach(item => {
                    item.valueId = item.defaultSpecValue.valueId;
                    item.specName = item.defaultSpecValue.specName;
                });
                vc.http.apiPost(
                    '/assetInventory/saveAssetInventory',
                    JSON.stringify($that.assetInventoryInInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast(_json.msg);
                            vc.goBack();
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _openChooseProductModal: function () {
                if (!$that.assetInventoryInInfo.shId) {
                    vc.toast('请先选择仓库');
                    return;
                }
                vc.emit('chooseProductAndSpec', 'openChooseProductModel', {
                    shId: $that.assetInventoryInInfo.shId
                });
            },
            _goBack: function () {
                vc.goBack();
            },
            _deleteProduct: function (_inoutOrderProduct) {
                let _inoutOrderProducts = $that.assetInventoryInInfo.inoutOrderProducts;
                let index = _inoutOrderProducts.indexOf(_inoutOrderProduct);
                if (index > -1) {
                    _inoutOrderProducts.splice(index, 1);
                }
            },
            _listShopHouses: function (_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopHouse.queryShopHouse',
                    param,
                    function (json, res) {
                        var _shopHouseManageInfo = JSON.parse(json);
                        $that.assetInventoryInInfo.shopHouses = _shopHouseManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _caculTotalMount: function () {
                let j = 0;
                $that.assetInventoryInInfo.inoutOrderProducts.forEach(item => {
                    if (!isNaN(item.count) && !isNaN(item.price)) {
                        j += parseFloat(item.count) * parseFloat(item.price).toFixed(2);
                    } else {
                        $that.assetInventoryInInfo.totalMount = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.assetInventoryInInfo.totalMount = j.toFixed(2);
            },
            _computeData: function (_inoutOrderProduct) {
                _inoutOrderProduct.invProfit = parseInt(_inoutOrderProduct.invQuantity) - parseInt(_inoutOrderProduct.quantity);
                if (_inoutOrderProduct.invProfit < 0) {
                    _inoutOrderProduct.invProfit = 0;
                }
                _inoutOrderProduct.invLoss = parseInt(_inoutOrderProduct.quantity) - parseInt(_inoutOrderProduct.invQuantity);
                if (_inoutOrderProduct.invLoss < 0) {
                    _inoutOrderProduct.invLoss = 0;
                }
                _inoutOrderProduct.invProfitMoney = _inoutOrderProduct.invProfit * parseFloat(_inoutOrderProduct.price);
                _inoutOrderProduct.invLossMoney = _inoutOrderProduct.invLoss * parseFloat(_inoutOrderProduct.price);
                $that.$forceUpdate();
            }
        }
    });
})(window.vc);
